/*
 * @Author: viivLgr 
 * @Date: 2018-03-22 12:38:56 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-22 17:05:17
 */
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _util = require('util/util.js');
var Pagination = require('util/pagination/index.js');
var _product = require('service/product-service.js');
var templateIndex = require('./index.string');

var page = {
    data: {
        listParam: {
            keyword: _util.getUrlParam('keyword') || '',
            categoryId: _util.getUrlParam('categoryId') || '',
            orderBy: _util.getUrlParam('orderBy') || 'default',
            pageNum: _util.getUrlParam('pageNum') || 1,
            pageSize: _util.getUrlParam('pageSize') || 10
        }
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadList();
    },
    bindEvent: function () {
        var _this = this;
        // 排序的点击事件
        $('.sort-item').click(function () {
            var $this = $(this);
            _this.data.listParam.pageNum = 1;
            // 默认排序
            if ($this.data('type') === 'default') {
                if ($this.hasClass('active')) {
                    // 已经默认排序
                    return;
                } else {
                    $this.addClass('active')
                        .siblings('.sort-item').removeClass('active asc desc');
                    _this.data.listParam.orderBy = 'default';
                }
            } else if ($this.data('type') === 'price') {
                // 价格排序
                $this.addClass('active')
                    .siblings('.sort-item').removeClass('active asc desc');
                if(!$this.hasClass('asc')){
                    $this.addClass('asc').removeClass('desc');
                    _this.data.listParam.orderBy = 'price_asc';
                }else{
                    $this.addClass('desc').removeClass('asc');
                    _this.data.listParam.orderBy = 'price_desc';
                }
            }
            // 重新加载列表
            _this.loadList();
        });
    },
    // 加载list
    loadList: function () {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.p-list-con');;
        
            $pListCon.html('<div class="loading"></div>');
        // 删除参数中不必要的字段
        listParam.categoryId ? 
            (delete listParam.keyword) : (delete listParam.categoryId);
        
        _product.getProductList(listParam, function (res) {
            listHtml = _util.renderHtml(templateIndex, {
                list: res.list
            });
            $pListCon.html(listHtml);
            _this.loadPagination({
                hasPreviousPage: res.hasPreviousPage,
                prePage: res.prePage,
                hasNextPage: res.hasNextPage,
                nextPage: res.nextPage,
                pageNum: res.pageNum,
                pages: res.pages
            });
        }, function (err) {
            _util.errorTips(err);
        });
    },
    // 加载分页信息
    loadPagination: function (pageInfo) {
        var _this = this;
        _this.pagination ? '' : (_this.pagination = new Pagination());
        _this.pagination.render($.extend({}, pageInfo, {
            container: $('.pagination'),
            onSelectPage: function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadList();
            }
        }));
    }
};

$(function () {
    page.init();
});